package com.waghost.bridalgown.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.waghost.bridalgown.dao.UserInfoDao;
import com.waghost.bridalgown.entity.Result;
import com.waghost.bridalgown.entity.UserInfo;
import com.waghost.bridalgown.service.UserInfoService;
import lombok.AllArgsConstructor;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

@AllArgsConstructor
@Service
public class IUserInfoService implements UserInfoService {

    private UserInfoDao userInfoDao;
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result getUserInfo(Integer userId) {
        return Result.success(userInfoDao.selectById(userId));
    }

    @Override
    public Result login(String account, String password) {
        UserInfo user = userInfoDao.selectOne(new QueryWrapper<UserInfo>().eq("account", account));
        System.out.println(user);
        if (null == user) {
            return Result.error("用户不存在在");
        }
        if (!user.getPassword().equals(password)) {
            return Result.error("密码不正确");
        }
        String token = UUID.randomUUID().toString();
        // 走缓存
        System.out.println(JSONObject.toJSONString(user)+":::ss");
        System.out.println(token);
        stringRedisTemplate.opsForValue().set(token, JSONObject.toJSONString(user));
        return Result.success(token, "登录成功");
    }

    @Override
    public Result listUsers() {
        return Result.success(userInfoDao.selectList(new QueryWrapper<UserInfo>().eq("role",2)));
    }

    @Override
    public Result addUser(UserInfo userInfo) {
        // 判断账号是否存在
        if (null != userInfoDao.selectOne(new QueryWrapper<UserInfo>().eq("account", userInfo.getAccount()))) {
            return Result.error("账号已存在");
        }
        return Result.toAjax(userInfoDao.insert(userInfo), "添加成功", "添加错误");
    }

    @Override
    public Result updateUserInfo(UserInfo userInfo) {
        return Result.toAjax(userInfoDao.updateById(userInfo), "修改成功", "修改错误");

    }

    @Override
    public Result deleteUser(Integer userId) {
        return Result.toAjax(userInfoDao.deleteById(userId), "删除成功", "删除错误");

    }

    @Override
    public Result listUsersTop() {
        return Result.success(userInfoDao.selectList(new QueryWrapper<UserInfo>().in("role", new String[]{"3", "4", "5"})));
    }

    @Override
    public Result getUser(Integer id) {

        return Result.success(userInfoDao.selectById(id));
    }

    @Override
    public Result resetPwd(UserInfo userInfo) {
        if (userInfo.getAccount() == null||userInfo.getPassword() == null){
            return Result.error("账号或密码不能为空");
        }
        UserInfo account = userInfoDao.selectOne(new QueryWrapper<UserInfo>().eq("account", userInfo.getAccount()));
        if (account == null){
            return Result.error("账号不存在");
        }
        userInfo.setId(account.getId());
        int i = userInfoDao.updateById(userInfo);
        return Result.toAjax(i, "修改成功", "修改错误");
    }
}
